package com.ethink.platform.controller.order;

import com.ethink.framework.common.web.context.SPRequestContextHolder;
import com.ethink.order.api.response.TocSaleAfterImportLogGetResponse;
import com.ethink.order.api.response.TocSaleAfterImportResponse;
import com.ethink.platform.controller.order.info.TocSaleAfterImportInfo;
import com.ethink.platform.manager.TocSaleAfterManager;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import com.ethink.framework.common.web.BaseController;
import com.ethink.framework.common.web.response.CommonResponse;
import com.ethink.order.api.TocSaleAfterImportLogFacade;
import com.ethink.order.api.info.TocSaleAfterImportLogInfo;
import com.ethink.order.api.request.TocSaleAfterImportLogAddRequest;
import com.ethink.order.api.request.TocSaleAfterImportLogQueryRequest;
import org.springframework.web.multipart.MultipartFile;

/**
 * TOC售后导入日志 Controller
 *
 * @author default
 * @date 2023-01-10
 */
@Tag(name = "TocSaleAfterImportLogController", description = "TOC售后导入日志 Controller")
@RestController
@Slf4j
@RequestMapping("platform-server/v1/order-service")
public class TocSaleAfterImportLogController extends BaseController {

    @Autowired
    private TocSaleAfterImportLogFacade tocSaleAfterImportLogFacade;

    @Autowired
    private TocSaleAfterManager tocSaleAfterManager;

    @Operation(summary = "分页查询")
    @GetMapping("/tocSaleAfterImportLog/pageQuery")
    public CommonResponse<List<TocSaleAfterImportLogInfo>> queryTocSaleAfterImportLog(TocSaleAfterImportLogQueryRequest request) {
        return succeedWithPage(tocSaleAfterImportLogFacade.queryTocSaleAfterImportLog(request));
    }

    @Operation(summary = "详情查询")
    @GetMapping("/tocSaleAfterImportLog/get/{id}")
    public CommonResponse<TocSaleAfterImportLogGetResponse> getTocSaleAfterImportLogById(@PathVariable("id") Long id) {
        return succeedWith(tocSaleAfterImportLogFacade.getTocSaleAfterImportLogById(id));
    }

    @Operation(summary = "新增")
    @PostMapping("/tocSaleAfterImportLog/add")
    public CommonResponse addTocSaleAfterImportLog(@Validated @RequestBody TocSaleAfterImportLogAddRequest request) {
        request.setCreateUserCode(SPRequestContextHolder.getCurrentSystemUserLoginName());
        request.setCreateUserName(SPRequestContextHolder.getCurrentSystemUserRealName());
        tocSaleAfterImportLogFacade.addTocSaleAfterImportLog(request);
        return succeed();
    }

    @Operation(summary = "删除")
    @PutMapping("/tocSaleAfterImportLog/delete/{id}")
    public CommonResponse deleteTocSaleAfterImportLog(@PathVariable("id") Long id) {
        tocSaleAfterImportLogFacade.deleteTocSaleAfterImportLog(id);
        return succeed();
    }

    @Operation(summary = "导入")
    @PostMapping("/tocSaleAfterImportLog/import")
    public CommonResponse<List<TocSaleAfterImportInfo>> importTocSaleAfterImportLog(@RequestParam("file") MultipartFile file,
                                                                                    @RequestParam("orderType") Integer orderType) {
        List<TocSaleAfterImportInfo> importInfos = tocSaleAfterManager.excelImport(file, orderType);
        return succeedWith(importInfos);
    }

    @Operation(summary = "匹配确认")
    @PutMapping("/tocSaleAfterImportLog/match/{id}")
    public CommonResponse tocSaleAfterImportLogConfirm(@PathVariable("id") Long id) {
        List<TocSaleAfterImportResponse> responseList = tocSaleAfterImportLogFacade.settleOrder(id);
        tocSaleAfterManager.createInventoryOrder(responseList, id);
        return succeed();
    }
}